<!-- 综合能效水平分析 -->

<template>
  <div class="flex flex-col w-100% p-10px box-border" style="height: calc(100% - 40px)">
    <!-- card-1 -->
    <div
      class="flex flex-col mb-10px"
      style="height: calc(50% - 10px); box-shadow: 0px 1px 10px 0px rgba(153, 153, 153, 0.4)"
    >
      <div class="flex justify-between items-center h-50px pl-10px pr-40px bg-[#F8F8F8]">
        <span class="text-[#333] text-16px font-bold">石化基地综合能效概览</span>

        <div class="flex items-center">
          <span class="mr-11px text-[#333] text-14px">选择时间</span>
          <el-date-picker
            v-model="echartsDate"
            type="daterange"
            range-separator="至"
            unlink-panels
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            @change="dateChangeFormat(echartsDate)"
          />
          <el-button
            type="primary"
            class="w-80px ml-50px rounded-15px"
            :icon="Search"
            @click="loadEchart"
            >查询</el-button
          >
          <el-button class="w-80px bg-[#dceeff] rounded-15px" :icon="Refresh" @click="resetEchart"
            >重置</el-button
          >
        </div>
      </div>
      <div class="w-100% flex-1 flex">
        <div class="w-300px flex flex-col justify-center items-center">
          <div
            class="w-260px h-170px mb-20px bg-[rgba(220,238,255,0.4)] rounded-10px"
            style="border: 1px solid #0f86e8"
          >
            <div class="text-20px font-bold text-[#333] text-center mt-20px">
              园区单位产值综合能耗
            </div>
            <div class="text-center">
              <span class="text-30px text-[#0F86E8] pr-4px" style="font-family: Digiface">{{
                cardForm.dwzhnh
              }}</span>
              <span class="text-14px text-[#0F86E8]">万tce/元</span>
            </div>
            <div class="relative flex items-center mt-10px ml-17px mr-20px">
              <span
                class="inline-block w-70px h-24px lh-24px mr-40px bg-[#8893A5] rounded-12px text-[#fff] text-center font-bold"
                >同比</span
              >
              <span v-if="cardForm.dwzhnhTbz > 0" class="state-top-text"
                >{{ cardForm.dwzhnhTbz }}%</span
              >
              <span v-else-if="cardForm.dwzhnhTbz < 0" class="state-bottom-text"
                >{{ cardForm.dwzhnhTbz }}%</span
              >
              <span v-else-if="cardForm.dwzhnhTbz" class="state-top-text" style="color: #000"
                >{{ cardForm.dwzhnhTbz }}%</span
              >
              <span v-else class="state-top-text" style="color: #000">无</span>
              <i v-if="cardForm.dwzhnhTbz > 0" class="state-top-icon"></i>
              <i v-else-if="cardForm.dwzhnhTbz < 0" class="state-bottom-icon"></i>
            </div>
            <div class="relative flex items-center mt-10px ml-17px mr-20px">
              <span
                class="inline-block w-70px h-24px lh-24px mr-40px bg-[#8893A5] rounded-12px text-[#fff] text-center font-bold"
                >环比</span
              >
              <span v-if="cardForm.dwzhnhHbz > 0" class="state-top-text"
                >{{ cardForm.dwzhnhHbz }}%</span
              >
              <span v-else-if="cardForm.dwzhnhHbz < 0" class="state-bottom-text"
                >{{ cardForm.dwzhnhHbz }}%</span
              >
              <span v-else-if="cardForm.dwzhnhHbz" class="state-top-text" style="color: #000"
                >{{ cardForm.dwzhnhHbz }}%</span
              >
              <span v-else class="state-top-text" style="color: #000">无</span>
              <i v-if="cardForm.dwzhnhHbz > 0" class="state-top-icon"></i>
              <i v-else-if="cardForm.dwzhnhHbz < 0" class="state-bottom-icon"></i>
            </div>
          </div>
          <div
            class="w-260px h-170px bg-[rgba(220,238,255,0.4)] rounded-10px"
            style="border: 1px solid #0f86e8"
          >
            <div class="text-20px font-bold text-[#333] text-center mt-20px">园区单位产值电耗</div>
            <div class="text-center">
              <span class="text-30px text-[#0F86E8] pr-4px" style="font-family: Digiface">{{
                cardForm.dwczdh
              }}</span>
              <span class="text-14px text-[#0F86E8]">万kWh/元</span>
            </div>
            <div class="relative flex items-center mt-10px ml-17px mr-20px">
              <span
                class="inline-block w-70px h-24px lh-24px mr-40px bg-[#8893A5] rounded-12px text-[#fff] text-center font-bold"
                >同比</span
              >
              <span v-if="cardForm.dwczdhTbz > 0" class="state-top-text"
                >{{ cardForm.dwczdhTbz }}%</span
              >
              <span v-else-if="cardForm.dwczdhTbz < 0" class="state-bottom-text"
                >{{ cardForm.dwczdhTbz }}%</span
              >
              <span v-else-if="cardForm.dwczdhTbz" class="state-top-text" style="color: #000"
                >{{ cardForm.dwczdhTbz }}%</span
              >
              <span v-else class="state-top-text" style="color: #000">无</span>
              <i v-if="cardForm.dwczdhTbz > 0" class="state-top-icon"></i>
              <i v-else-if="cardForm.dwczdhTbz < 0" class="state-bottom-icon"></i>
            </div>
            <div class="relative flex items-center mt-10px ml-17px mr-20px">
              <span
                class="inline-block w-70px h-24px lh-24px mr-40px bg-[#8893A5] rounded-12px text-[#fff] text-center font-bold"
                >环比</span
              >
              <span v-if="cardForm.dwczdhHbz > 0" class="state-top-text"
                >{{ cardForm.dwczdhHbz }}%</span
              >
              <span v-else-if="cardForm.dwczdhHbz < 0" class="state-bottom-text"
                >{{ cardForm.dwczdhHbz }}%</span
              >
              <span v-else-if="cardForm.dwczdhHbz" class="state-top-text" style="color: #000"
                >{{ cardForm.dwczdhHbz }}%</span
              >
              <span v-else class="state-top-text" style="color: #000">无</span>
              <i v-if="cardForm.dwczdhHbz > 0" class="state-top-icon"></i>
              <i v-else-if="cardForm.dwczdhHbz < 0" class="state-bottom-icon"></i>
            </div>
          </div>
        </div>
        <div class="flex-1">
          <Echarts ref="chartRef"></Echarts>
        </div>
      </div>
    </div>

    <!-- card-2 -->
    <div
      class="flex flex-col"
      style="height: calc(50%); box-shadow: 0px 1px 10px 0px rgba(153, 153, 153, 0.4)"
    >
      <div class="w-100% h-50px lh-50px pl-10px bg-[#F8F8F8]">
        <span class="text-[#333] text-16px font-bold">各企业综合能效列表</span>
      </div>
      <div class="w-100% flex-1 box-border" style="height: calc(100% - 50px)">
        <el-form :inline="true" class="h-80px ml-40px mr-40px flex justify-between items-center">
          <el-form-item label="选择企业" style="margin-bottom: 0">
            <el-select v-model="qymc" placeholder="请选择企业" class="w-240px">
              <el-option
                v-for="item in qyOption"
                :key="item.value"
                :label="item.text"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="选择指标名称" style="margin-bottom: 0">
            <el-select v-model="zbmc" placeholder="请选择指标名称" class="w-160px">
              <el-option
                v-for="item in zbmcOption"
                :key="item.dm"
                :label="item.mc"
                :value="item.dm"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="选择时间范围" style="margin-bottom: 0">
            <el-date-picker
              v-model="tableDate"
              type="daterange"
              range-separator="至"
              unlink-panels
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              @change="dateChangeFormat(tableDate)"
            />
          </el-form-item>
          <div class="mt-2px text-right">
            <el-button
              type="primary"
              class="w-80px rounded-15px"
              :icon="Search"
              @click="loadTableData"
              >查询</el-button
            >
            <el-button
              class="w-80px bg-[#dceeff] rounded-15px"
              :icon="Refresh"
              @click="resetTableSearch"
              >重置</el-button
            >
          </div>
        </el-form>

        <Table
          class="mt-20px"
          style="height: calc(100% - 150px)"
          :table-columns="tableColumns"
          :table-data="tableData"
        >
          <template #zbmcColumn="{ row }">
            <span>
              {{ formatZbmc(row.zbmc) }}
            </span>
          </template>

          <template #actionColumn="{ row }">
            <el-button link type="primary" @click="queryInfo(row)">查看</el-button>
          </template>
        </Table>

        <el-pagination
          class="justify-end m-10px mt-15px"
          layout="total, sizes, prev, pager, next"
          :total="tableTotal"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>

  <Dialog
    :visible="dialogShow"
    title="企业综合能效详情查看"
    width="1000px"
    @close="dialogShow = false"
  >
    <el-form
      v-for="(formItem, formIndex) in qyzhnxFormList"
      :key="formIndex"
      :label-width="formItem.formLabelWidth"
    >
      <el-row :gutter="0">
        <el-col v-for="(item, index) in formItem.formList" :key="index" :span="item.span || 12">
          <el-form-item :label="item.label">
            <el-input v-model="item.value" :type="item.type" :disabled="inputDisabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </Dialog>
</template>

<script setup lang="ts">
import { Search, Refresh } from '@element-plus/icons-vue'
import moment from 'moment'
import { dateChangeFormat } from '@/utils/date'

/** echarts start */
const echartsDate = ref([
  moment().startOf('month').format('YYYY-MM-DD HH:mm:ss'),
  moment().format('YYYY-MM-DD HH:mm:ss'),
])

const cardForm: any = reactive({
  dwzhnh: '0',
  dwzhnhTbz: '0',
  dwzhnhHbz: '0',
  dwczdh: '0',
  dwczdhTbz: '0',
  dwczdhHbz: '0',
})
function loadCardInfo() {
  const params = {
    nylx: '2',
    startTime: echartsDate.value[0],
    endTime: echartsDate.value[1],
    ymlx: 1,
  }
  queryYqDwCzInfo(params).then((res: any) => {
    cardForm.dwzhnh = res.dwzhnh
    cardForm.dwzhnhTbz = res.dwzhnhTbz
    cardForm.dwzhnhHbz = res.dwzhnhHbz
    cardForm.dwczdh = res.dwczdh
    cardForm.dwczdhTbz = res.dwczdhTbz
    cardForm.dwczdhHbz = res.dwczdhHbz
  })
}
loadCardInfo()

const chartRef: any = ref(null)
const chartOption: any = {
  grid: {
    left: '8%',
    right: '8%',
    top: '14%',
    bottom: '8%',
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      animation: false,
    },
    backgroundColor: 'rgba(16,16,52,0.6)',
    textStyle: {
      color: 'white',
    },
  },
  xAxis: [
    {
      type: 'category',
      data: [],
      axisPointer: {
        type: 'shadow',
      },
    },
  ],
  yAxis: [
    {
      position: 'left', // 将 y 轴设置为左侧显示
      axisLabel: {
        formatter: '{value}', // 显示刻度标签数值
      },
    },
    {
      position: 'right', // 将 y 轴设置为右侧显示
      min: 0,
      axisLabel: {
        formatter: '{value}', // 显示刻度标签数值
      },
    },
  ],
  graphic: [],

  legend: {
    data: ['单位产值综合能耗', '单位产值电耗'],
    left: 'center',
  },
  series: [
    {
      name: '单位产值综合能耗',
      type: 'line',
      itemStyle: {
        normal: {
          color: '#3AB779',
          lineStyle: {
            color: '#3AB779FF',
          },
        },
      },
      data: [],
    },
    {
      name: '单位产值电耗',
      type: 'line',
      itemStyle: {
        normal: {
          color: '#0F86E8',
          lineStyle: {
            color: '#0F86E8',
          },
        },
      },
      data: [],
    },
  ],
}

onMounted(() => {
  chartOption.graphic = [
    {
      type: 'text',
      left: 0,
      style: {
        text: '单位产值综合能耗 tce/元',
        textAlign: 'center',
        fill: '#000',
        fontSize: 14,
      },
    },
    {
      type: 'text',
      right: 40,
      style: {
        text: '单位产值电耗 kWh/元',
        textAlign: 'center',
        fill: '#000',
        fontSize: 14,
      },
    },
    {
      type: 'text',
      left: 'center',
      top: 'middle',
      style: {
        fill: '#999',
        text: '暂无数据',
        font: '14px Microsoft YaHei',
      },
    },
  ]
  chartRef.value.initEchat(chartOption)
})

function loadEchart() {
  const params = {
    nylx: '2',
    startTime: echartsDate.value[0],
    endTime: echartsDate.value[1],
    ymlx: 1,
  }
  queryYqDwCzInfoList(params).then((res: any) => {
    res.dwHistoryInfoList.forEach((item: any) => {
      if (item.ylx === '单位产值综合能耗') {
        chartOption.series[0].data = item.ylz ? item.ylz : []
      } else {
        chartOption.series[1].data = item.ylz ? item.ylz : []
      }
    })

    chartOption.graphic = []
    if (res.sjList.length < 1) {
      chartOption.graphic.push({
        type: 'text',
        left: 'center',
        top: 'middle',
        style: {
          fill: '#999',
          text: '暂无数据',
          font: '14px Microsoft YaHei',
        },
      })
    } else {
      chartOption.graphic = [
        {
          type: 'text',
          left: 0,
          style: {
            text: '单位产值综合能耗 tce/元',
            textAlign: 'center',
            fill: '#000',
            fontSize: 14,
          },
        },
        {
          type: 'text',
          right: 40,
          style: {
            text: '单位产值电耗 kWh/元',
            textAlign: 'center',
            fill: '#000',
            fontSize: 14,
          },
        },
      ]
    }
    chartOption.xAxis[0].data = res.sjList ? res.sjList : []
    chartRef.value.initEchat(chartOption)
  })
}
loadEchart()

const resetEchart = () => {
  echartsDate.value = [
    moment().startOf('month').format('YYYY-MM-DD HH:mm:ss'),
    moment().format('YYYY-MM-DD HH:mm:ss'),
  ]

  loadEchart()
}
/** echarts end */

//查询字典
const zbmcOption: any = ref([]) //指标名称
const qyOption: any = ref([]) //企业
function getDictionary() {
  queryZdInfo({ zdxdm: 'zbmc' }).then((res: any) => {
    zbmcOption.value = res
  })

  queryQymcInfoList().then((res: any) => {
    qyOption.value = res
  })
}
getDictionary()

/** table start */
//处理指标名称
const formatZbmc = computed(() => {
  return (zbmc: any) => {
    if (zbmc) {
      let result = ''
      zbmcOption.value.forEach((item: any) => {
        if (item.dm === zbmc) {
          result = item.mc
        }
      })
      return result
    }
    return ''
  }
})

const qymc = ref('')
const zbmc = ref('')
const tableDate = ref(['', ''])
const pageNo = ref(1)
const pageSize = ref(10)
const tableTotal = ref(0)
function loadTableData() {
  const params = {
    qymc: qymc.value,
    zbmc: zbmc.value,
    startTime: tableDate.value[0],
    endTime: tableDate.value[1],
    pageNo: pageNo.value,
    pageSize: pageSize.value,
  }
  queryNxzbpzQyInfoList(params).then((res: any) => {
    tableData.value = res.records
    pageNo.value = res.current
    pageSize.value = res.size
    tableTotal.value = res.total
  })
}
loadTableData()

//分页按钮
const handleSizeChange = (val: number) => {
  console.log(`每页 ${val} 条`)
  pageSize.value = val
  loadTableData()
}
const handleCurrentChange = (val: number) => {
  console.log(`当前页: ${val}`)
  pageNo.value = val
  loadTableData()
}

const resetTableSearch = () => {
  qymc.value = ''
  zbmc.value = ''
  tableDate.value = ['', '']

  loadTableData()
}

//查询弹窗信息
const queryInfo = (row: any) => {
  queryNxzbpzQyInfo({ nxId: row.id }).then((res: any) => {
    for (const key in res) {
      qyzhnxFormList[0].formList.forEach((item: any) => {
        if (key === item.prop) {
          //指标名称查询字典值
          if (item.prop === 'zbmc') {
            let result = ''
            zbmcOption.value.forEach((item: any) => {
              if (item.dm === res[key]) {
                result = item.mc
              }
            })
            item.value = result
          } else {
            item.value = res[key]
          }
        }
      })
    }
    dialogShow.value = true
  })
}
const dialogShow = ref(false)
const inputDisabled = ref(true)
const qyzhnxFormList = [
  {
    formLabelWidth: '160px',
    formList: [
      {
        prop: 'qyMc',
        label: '企业名称',
        type: 'input',
        value: '',
      },
      {
        prop: 'zbmc',
        label: '指标名称',
        type: 'input',
        value: '',
      },
      {
        prop: 'zblx',
        label: '指标类型',
        type: 'input',
        value: '',
      },
      {
        prop: 'tjkj',
        label: '统计口径',
        type: 'input',
        value: '',
      },
      {
        prop: 'jszq',
        label: '计算周期',
        type: 'input',
        value: '',
      },
      {
        prop: 'zbz',
        label: '指标值',
        type: 'input',
        value: '',
      },
      {
        prop: 'gxsj',
        label: '指标单位更新时间',
        type: 'input',
        value: '',
      },
      {
        prop: 'tbz',
        label: '指标同比',
        type: 'input',
        value: '',
      },
      {
        prop: 'hbz',
        label: '指标环比',
        type: 'input',
        value: '',
      },
    ],
  },
]

// 定义列配置
const tableColumns = [
  {
    label: '序号',
    width: '80',
  },
  {
    prop: 'qyMc',
    label: '企业名称',
  },
  {
    slotName: 'zbmcColumn',
    type: 'slot',
    label: '指标名称',
  },
  {
    prop: 'zblx',
    label: '指标类型',
  },
  {
    prop: 'tjkj',
    label: '统计口径',
    width: '300',
  },
  {
    prop: 'jszq',
    label: '计算周期',
    width: '100',
  },
  {
    prop: 'zbz',
    label: '指标值',
    width: '80',
  },
  {
    prop: 'gxsj',
    label: '指标单位更新时间',
    width: '180',
  },
  {
    prop: 'tbz',
    label: '指标同比',
    width: '100',
  },
  {
    prop: 'hbz',
    label: '指标环比',
    width: '100',
  },
  {
    label: '操作',
    width: '100',
    slotName: 'actionColumn',
    type: 'slot',
  },
]

// 定义表格数据源
const tableData = ref([])
/** table end */
</script>

<style scoped>
.state-top-icon {
  display: inline-block;
  width: 20px;
  height: 20px;
  position: absolute;
  right: 0;
  background-image: url('@/assets/image/compEfficiencyAnalysis/top-icon.png');
  background-size: 100% 100%;
}

.state-top-text {
  color: #ff3333;
  font-size: 16px;
  font-weight: 700;
}

.state-bottom-icon {
  display: inline-block;
  width: 20px;
  height: 20px;
  position: absolute;
  right: 0;
  background-image: url('@/assets/image/compEfficiencyAnalysis/bottom-icon.png');
  background-size: 100% 100%;
}

.state-bottom-text {
  color: #17ac11;
  font-size: 16px;
  font-weight: 700;
}
</style>
